﻿@{
    Layout = null;
    DeliveryManage.Models.ReturnMsgModel returnMsg = ViewBag.ReturnMsgModel;
}
@model DeliveryManage.Model.Sys_PerformanceRules
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>编辑业绩计算规则</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no" />
    <link rel="stylesheet" href="~/Content/font.css">
    <link rel="stylesheet" href="~/Content/framemain.css">
    <link rel="stylesheet" href="~/Content/pagination.css" />
    <script type="text/javascript" src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="~/Content/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="~/Scripts/framemain.js"></script>
    <script type="text/javascript" src="~/Scripts/laymain.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
    <style>
        .layui-table img {
            max-width: inherit;
        }

        .pro-img {
            border-radius: 5px;
            border: 1px solid #f2f2f2;
        }

        .layui-layer-molv .layui-layer-btn .layui-layer-btn1 {
            background: #fff !important;
            border: 1px solid #dedede;
        }
    </style>
    <script type="text/javascript">
    $(function () {
        //操作提示
        if(@returnMsg.code== 400){
            layer.alert('@returnMsg.msg', { icon: 5, title: '提示' }, function (index) {
            })
        }
        $(document).ready(function () {
            $("#myfrom").submit(function () {
                //获取table中设置的规则
                var rulestr = GetAllRules();
                $("#RulesString").val(rulestr);
                //layer.alert(rulestr);
                var data = $(this).serialize();
                layer.confirm('确认保存吗?', { icon: 3, title: '提示' }, function (index) {
                    $.ajax({
                        type: "POST",
                        url: "/System/SalaryCalcRulesToAdd",
                        data: data,
                        success: function (obj) {
                            if (obj.status == true) {
                                layer.msg(obj.msg, { icon: 6, title: '提示' }, function (index) {
                                    //window.parent.window.location.href = '/User/UserList';
                                });
                            } else {
                                layer.confirm(obj.msg, { icon: 5, title: '提示' }, function (index) {
                                    layer.close();
                                    location.reload();
                                });
                            }
                        }
                    })
                    layer.close();
                });
                return false;

            })
        })
    });
    </script>
</head>
<body>
    <form id="myfrom" class="tab-content layui-form" action="" lay-filter="example">
        @{
            @Html.HiddenFor(m => m.Id)
            @Html.Hidden("AddOrEdit", (string)ViewBag.AddOrEdit)
            @Html.Hidden("RulesString", "")
            if (returnMsg.code == 200)
            {
                <div class="layui-fluid">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-md12">
                            <div class="layui-card">
                                <div class="layui-card-body ">
                                    <blockquote class="layui-elem-quote">规则基本信息</blockquote>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">规则名字</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(m => m.Name, new { @datatype = "s2-30", @autocomplete = "off", @class = "layui-input", @sucmsg = " ", id = "CustomerName", placeholder = "请输入规则名字" })
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">适配员工</label>
                                        <div class="layui-input-inline">
                                            <input type="radio" name="ToType" value="false" lay-filter="labeType" lay-verify="required" title="无底薪" @(Model.ToType == 0 ? "checked=\"checked\"" : "")>
                                            <input type="radio" name="ToType" value="true" lay-filter="labeType" lay-verify="required" title="有底薪" @(Model.ToType == 1 ? "checked=\"checked\"" : "")>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">是否启用</label>
                                        <div class="layui-input-inline">
                                            <input type="radio" name="IsEnlabe" value="false" lay-filter="labeType" lay-verify="required" title="关闭" @(Model.IsEnlabe == false ? "checked=\"checked\"" : "")>
                                            <input type="radio" name="IsEnlabe" value="true" lay-filter="labeType" lay-verify="required" title="启用" @(Model.IsEnlabe == true ? "checked=\"checked\"" : "")>
                                        </div>
                                    </div>
                                    <blockquote class="layui-elem-quote">
                                        规则详情
                                        <a class="layui-btn" onclick="AddOneRules();" style="float:right"><i class="layui-icon">&#xe61f;</i>添加规则</a>
                                    </blockquote>
                                    <table class="layui-table layui-form">
                                        <thead>
                                            <tr>
                                                <th>下限值<span style="font-size:small;color:red">(最低业绩设置0)</span></th>
                                                <th>上限值<span style="font-size: small; color: red">(无上限请设置为0)</span></th>
                                                <th>提成比例%</th>
                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tbody id="HasRulseInfo">
                                            @if (ViewBag.RulesList.Count > 0)
                                            {
                                                foreach (DeliveryManage.Model.Sys_PerformanceRules item in ViewBag.RulesList)
                                                {
                                                    <tr>
                                                        <td><input type="number" class="layui-input" value="@item.MinLimit" /></td>
                                                        <td><input type="number" class="layui-input" value="@item.MaxLimit" /></td>
                                                        <td><input type="number" class="layui-input" value="@item.CommRatio" /></td>
                                                        <td><a class="layui-btn" onclick="deleteThisRules(this);"><i class="layui-icon">&#xe640;</i>移除</a></td>
                                                    </tr>
                                                }
                                            }
                                            else
                                            {
                                                <tr id="NoRulesInfo">
                                                    <td colspan="4" align="center" height="80"><img src="~/images/nodata.png" style="height:60px;" /><br />请添加规则</td>
                                                </tr>
                                            }
                                        </tbody>
                                    </table>

                                    <div class="layui-input-inline">
                                        <input id="btnSubmit" name="btnSubmit" type="submit" value="保存规则" class="layui-btn savebtn" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            }
        }
    </form>
    <script type="text/javascript">
        layui.use(['form', 'layer'],
            function () {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer;
            });

        ///添加一条新规则
        function AddOneRules(selectGoodsId) {
            $("#NoRulesInfo").hide();
            var parentNode = document.getElementById("HasRulseInfo");
            var htmlToAppend = "<tr><td><input type=\"number\" class=\"layui-input\" value=\"\"/></td>"+
                                    "<td><input type=\"number\" class=\"layui-input\" value=\"\" /></td>"+
                                    "<td><input type=\"number\" class=\"layui-input\" value=\"\" /></td>"+
                "<td><a class=\"layui-btn\" onclick=\"deleteThisRules(this);\"><i class=\"layui-icon\">&#xe640;</i>移除</a></td></tr>";
            parentNode.innerHTML += htmlToAppend;
        }

        //移除掉一条新规则
        function deleteThisRules(element) {
            var parent = element.parentNode.parentNode;
            parent.remove();
        }

        //获取规则值
        function GetAllRules() {
            var result = "";
            var table = document.getElementById("HasRulseInfo"); 
            for (var i = 0; i < table.rows.length; i++) {
                //console.log("第" + i + "行");
                var optionval = "";
                // 遍历当前行的每一个单元格  
                for (var j = 0; j < table.rows[i].cells.length; j++) {
                    // 检查单元格中是否有input元素  
                    var input = table.rows[i].cells[j].getElementsByTagName("input")[0];
                    if (input) {
                        // 如果有，获取并打印其值  
                        optionval += input.value + "-";
                    }
                }
                result += optionval + ",";
            }
            //console.log(result);
            return result;
        }
    </script>
</body>
</html>
